<script setup>
import { ref,defineProps,defineEmits } from 'vue'
import { ElForm, ElFormItem, ElInput, ElButton } from 'element-plus'
import { useAuthStore } from '../stores/modules/auth'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'

const loginForm = ref({
  username: '',
  password: ''
})



const formRef = ref()
const authStore = useAuthStore()
const router = useRouter()
const props = defineProps({
  register: {
    type: Boolean,
    default: false
  }
})
const emit = defineEmits(['update:register'])
const validateUsername = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入用户名'))
  } else {
    callback()
  }
}

const validatePassword = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入密码'))
  } else if (value.length < 6) {
    callback(new Error('密码长度不能少于6位'))
  } else {
    callback()
  }
}

const rules = {
  username: [{ validator: validateUsername, trigger: 'blur' }],
  password: [{ validator: validatePassword, trigger: 'blur' }]
}

const handleLogin = async () => {
  try {
    await formRef.value.validate()
    await authStore.loginApi(loginForm.value)

    router.push('/chat')
  } catch (error) {
    throw error
  }
}

const handleRegister = () => {
  emit('update:register', false)
}
</script>

<template>
  <div class="login-container">
    <div class="login-card">
      <h2 class="login-title">账号登录</h2>
      <el-form
        ref="formRef"
        :model="loginForm"
        :rules="rules"
        label-position="top"
        @submit.prevent="handleLogin"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            clearable
            :prefix-icon="User"
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
            :prefix-icon="Lock"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            native-type="submit"
            class="login-btn"
            :loading="authStore.loading"
            type="primary"
          >
            立即登录
          </el-button>
        </el-form-item>

        <div class="login-actions">
          <el-checkbox label="记住密码" />
          <el-link @click="handleRegister">没有账户?立即注册</el-link>
          <el-link type="primary">忘记密码?</el-link>
        </div>
      </el-form>



    </div>
  </div>

</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: var(--color-background-soft);
}

.login-card {
  width: 420px;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  background: var(--color-background);
}

.login-title {
  margin-bottom: 30px;
  text-align: center;
  color: var(--el-text-color-primary);
  font-size: 24px;
  font-weight: 500;
}

.login-btn {
  width: 100%;
  height: 40px;
  font-size: 16px;
}

.login-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: var(--el-text-color-primary);
}

:deep(.el-input__inner) {
  height: 40px;
   color: var(--el-text-color-primary);
}
:deep(.el-input__wrapper){
  background: var(--el-fill-color-light);
  color: var(--el-text-color-primary);
  outline: none;
}

</style>
